package com.nclg.exam.controller;

import com.github.pagehelper.PageInfo;
import com.nclg.exam.dto.QuestionDto;
import com.nclg.exam.dto.QuestionListDto;
import com.nclg.exam.entity.LayResponse;
import com.nclg.exam.entity.TQuestion;
import com.nclg.exam.service.QuestionService;
import com.nclg.exam.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 查询题库
     * @param dto
     * @return
     */
    @GetMapping("/selectByPage")
    public JsonResult selectByPage(QuestionDto dto){
        PageInfo<QuestionListDto> list =  questionService.selectByPage(dto);
        LayResponse<QuestionListDto> layResponse = new LayResponse<>("0", "", list.getTotal(), list.getList());
        return JsonResult.success(layResponse);
    }

    /**
     * 添加题目
     * @param question
     * @return
     */
    @PostMapping("/insert")
    public JsonResult insert(TQuestion question){
        question.setQCreatetime(new Date());
        boolean isTrue = questionService.insert(question);
        if (!isTrue) return JsonResult.failure("添加失败");
        return JsonResult.success("添加成功");
    }

    /**
     * 修改题目状态
     * @param status
     * @param qid
     * @return
     */
    @PutMapping("/updateStatus/{status}/{qid}")
    public JsonResult updateStatus(@PathVariable String status, @PathVariable String qid){
        boolean isTrue = questionService.updateStatus(Long.valueOf(qid), status);
        if (!isTrue) return JsonResult.failure("修改失败");
        return JsonResult.success();
    }

    /**
     * 编辑题目
     * @param question
     * @return
     */
    @PutMapping("/update")
    public JsonResult update(TQuestion question){
        boolean isTrue = questionService.update(question);
        if (!isTrue) return JsonResult.failure("修改失败");
        return JsonResult.success("修改成功");
    }
}
